<?php
class Category extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->helper('url');
        if (file_exists('application/controllers/install.php'))
            redirect('install');
        $this->load->library('session');
        $this->load->helper('date');
        $this->load->model('category_model');

        if (!($this->session->userdata['login'])) {
            redirect('poster_admin/login');
        }

    }

    function index() {
        $data['query'] = $this->category_model->getAll();
        $data['title'] = '分类查看';
        $this->load->view('templates/poster_admin/header', $data);
        $this->load->view('poster_admin/category/index', $data);
        $this->load->view('templates/poster_admin/footer', $data);
    }

    function create() {
        $this->load->helper('form');
        $this->load->library('form_validation');

        $this->form_validation->set_rules('name', '分类名', 'required|is_unique[pr_category.category]');
        $data['title'] = '添加分类';
        $data['error'] = '';

        if ($this->form_validation->run() == FALSE) {
            $this->load->view('templates/poster_admin/header', $data);
            $this->load->view('poster_admin/category/create', $data);
            $this->load->view('templates/poster_admin/footer', $data);
        } else {
            if (!($this->category_model->insert())) {
                $data['error'] = "时间错误<br/>";
                $this->load->view('templates/poster_admin/header', $data);
                $this->load->view('poster_admin/category/create', $data);
                $this->load->view('templates/poster_admin/footer', $data);
            } else {
                redirect('poster_admin/category/', 'refresh');
            }
        }

    }

    function edit() {
        $this->load->helper('form');
        $this->load->library('form_validation');

        $this->form_validation->set_rules('name', '分类名', 'required');
        $data['title'] = '修改分类';
        $data['error'] = '';
        $data['query'] = $this->category_model->select($this->input->get('id'));

        if (empty($data['query'])){  // if $id not existed, redirect
            $data['title'] = '出错啦';
            $data['words'] = '查询id错误';
            $this->load->view('templates/poster_admin/header', $data);
            $this->load->view('poster_admin/404', $data);
            $this->load->view('templates/poster_admin/footer', $data);
        } else {
            $data['year'] = date('Y', $data['query']['deadline']);
            $data['month'] = date('m', $data['query']['deadline']);
            $data['day'] = date('d', $data['query']['deadline']);
            $data['hour'] = date('H', $data['query']['deadline']);
            $data['minute'] = date('i', $data['query']['deadline']);
            $data['second'] = date('s', $data['query']['deadline']);

            if ($this->form_validation->run() == FALSE) {
                $this->load->view('templates/poster_admin/header', $data);
                $this->load->view('poster_admin/category/edit', $data);
                $this->load->view('templates/poster_admin/footer', $data);
            } else {
                if (!($this->category_model->edit())) {
                    $data['error'] = "时间错误<br/>";
                    $this->load->view('templates/poster_admin/header', $data);
                    $this->load->view('poster_admin/category/create', $data);
                    $this->load->view('templates/poster_admin/footer', $data);
                } else {
                    redirect('poster_admin/category/', 'refresh');
                }
            }
        }
    }

    function delete() {
        $this->category_model->delete();
        redirect('poster_admin/category/', 'refresh');
    }
}